package com.definesys.smecrailtransit.module.MonthForm;

import android.content.Context;

import com.definesys.smecrailtransit.SmecManager.SmecKpManager.SmecKpUtils;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.BaseSubscriber;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.HttpResult;
import com.definesys.smecrailtransit.base.SmecBasePresenter;
import com.definesys.smecrailtransit.module.MonthForm.bean.CarePlanBean;
import com.definesys.smecrailtransit.module.MonthForm.bean.FaultLogBean;
import com.definesys.smecrailtransit.module.MonthForm.bean.MonthFormBean;
import com.definesys.smecrailtransit.module.MonthForm.bean.PositionBean;
import com.definesys.smecrailtransit.module.MonthForm.dialog.MonthFormDialog;
import com.definesys.smecrailtransit.utils.ToastyUtils;

import java.util.ArrayList;
import java.util.HashMap;

import rx.Observable;

/**
 * Created by xupeizuo on 2017/7/20.
 */

public class MonthFormPresenter extends SmecBasePresenter implements MonthFormContract {

    public MonthFormPresenter(Context mContext) {
        super(mContext);
    }

    @Override
    public void getMonthForm(HashMap<String, String> map) {
        Observable<HttpResult<MonthFormBean>> observable=smecRailService.monthForm(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<MonthFormBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(MonthFormFragment.GET_MONTH_FORM_ERROR,new Object());
            }

            @Override
            public void onNext(HttpResult<MonthFormBean> arrayListHttpResult) {
                MonthFormBean arrayList=arrayListHttpResult.getData();
                mRxbus.post(MonthFormFragment.GET_MONTH_FORM_SUCCESS,arrayList);
            }
        });
    }

    @Override
    public void getStationForm(HashMap<String, Object> map) {
        Observable<HttpResult<ArrayList<PositionBean>>> observable=smecRailService.stationForm(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<PositionBean>>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(MonthFormDialog.GET_STATION_FORM_FAILED,"");
            }

            @Override
            public void onNext(HttpResult<ArrayList<PositionBean>> arrayListHttpResult) {
                ArrayList<PositionBean> arrayList=arrayListHttpResult.getData();
                mRxbus.post(MonthFormDialog.GET_STATION_FORM_SUCCESS,arrayList);
            }
        });
    }

    @Override
    public void getFaultLog(HashMap<String, Object> map) {
        Observable<HttpResult<FaultLogBean>> observable=smecRailService.faultLog(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<FaultLogBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);

                if(map.get("type") == null || map.get("type").equals("")){
                    mRxbus.post(MonthFormDialog.GET_FAULT_LOG_FAILED,"");
                }else {
                    mRxbus.post(MonthFormDialog.GET_FAULT_LOG_FAILED1,"");
                }
            }

            @Override
            public void onNext(HttpResult<FaultLogBean> arrayListHttpResult) {
                FaultLogBean arrayList= arrayListHttpResult.getData();
                if(map.get("type") == null || map.get("type").equals("")){
                    mRxbus.post(MonthFormDialog.GET_FAULT_LOG_SUCCESS,arrayList);
                }else {
                    mRxbus.post(MonthFormDialog.GET_FAULT_LOG_SUCCESS1,arrayList);
                }
            }
        });
    }

    @Override
    public void getCarePlan(HashMap<String, Object> map) {
        Observable<HttpResult<ArrayList<CarePlanBean>>> observable=smecRailService.carePlan(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<CarePlanBean>>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(MonthFormDialog.GET_CARE_PLAN_FAILED,"");
            }

            @Override
            public void onNext(HttpResult<ArrayList<CarePlanBean>> arrayListHttpResult) {
                ArrayList<CarePlanBean> arrayList=arrayListHttpResult.getData();
                mRxbus.post(MonthFormDialog.GET_CARE_PLAN_SUCCESS,arrayList);
            }
        });
    }

    @Override
    public void sendEmail(HashMap<String, Object> map) {
        Observable<HttpResult> observable=smecRailService.sendEmail(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }

            @Override
            public void onNext(HttpResult arrayListHttpResult) {
                SmecKpUtils.kpDismiss();
                ToastyUtils.showNormalToast(mContext,"邮件已发送");
            }
        });
    }

}
